﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace ChuyenDongVatLy
{
    public partial class FrMPNDefault : Form
    {
        public FrMPNDefault()
        {
            InitializeComponent();
        }

        public delegate void GetString(string vantoc, string luc, string khoiluong,string hesomasat);
        public GetString GetMyValue;
        public delegate void GetString2(bool createfrom);
        public GetString2 GetMyValue2;              

        private void button1_Click(object sender, EventArgs e)
        {


            if (tbxKhoiluong.Text == "" || tbxLuc.Text == "" || tbxVantoc.Text == "" || tbxHeso.Text == "")
            {
                MessageBox.Show("Ban phai dien day du thong tin!");
            }
            else
            {
                float m = float.Parse(tbxKhoiluong.Text);
                float f = float.Parse(tbxLuc.Text);
                float v = float.Parse(tbxVantoc.Text);
                float k = float.Parse(tbxHeso.Text);

                if (m < 1 || m > 10 || f < 10 || f > 600 || v < 0 || v > 600 || k < 0 || k > 1)
                {
                    if (m < 1 || m > 10)
                        MessageBox.Show("Khối lương không phù hợp 1->10kg");

                    if (f < 10 || f > 600)
                    {
                        MessageBox.Show("Lực tác dụng không phù hợp 10->600N");
                    }
                    if (v < 0 || v > 600)
                        MessageBox.Show("Giá trị không phù hợp 0->600m/s");

                    if (k < 0 || k > 1)
                        MessageBox.Show("Hệ số ma sát không phù hợp 0->1");
                }
                else if (GetMyValue != null)
                {
                    GetMyValue(tbxVantoc.Text, tbxLuc.Text, tbxKhoiluong.Text, tbxHeso.Text);
                }

            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (GetMyValue2 != null)
            {
                GetMyValue2(true);
            }
            this.Close();
        }

        private void tbxVantoc_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            char decimalChar = Convert.ToChar(decimalString);

            if (Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)) { }
            else if (e.KeyChar == decimalChar && tbxVantoc.Text.IndexOf(decimalString) == -1)
            { }
            else
            {
                e.Handled = true;
            }           
        }

        private void tbxLuc_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            char decimalChar = Convert.ToChar(decimalString);

            if (Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)) { }
            else if (e.KeyChar == decimalChar && tbxLuc.Text.IndexOf(decimalString) == -1)
            { }
            else
            {
                e.Handled = true;
            }
        }

        private void tbxKhoiluong_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            char decimalChar = Convert.ToChar(decimalString);

            if (Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)) { }
            else if (e.KeyChar == decimalChar && tbxKhoiluong.Text.IndexOf(decimalString) == -1)
            { }
            else
            {
                e.Handled = true;
            }
        }

        private void tbxHeso_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            char decimalChar = Convert.ToChar(decimalString);

            if (Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)) { }
            else if (e.KeyChar == decimalChar && tbxHeso.Text.IndexOf(decimalString) == -1)
            { }
            else
            {
                e.Handled = true;
            }
        }

        private void btsave_Click(object sender, EventArgs e)
        {
            if (tbxHeso.Text == "" || tbxKhoiluong.Text == "" || tbxLuc.Text == "" || tbxVantoc.Text == "")
                MessageBox.Show("Cần điền đầy đủ thông sô !!!");
            else
            {
                SaveFileDialog saveFile = new SaveFileDialog();
                saveFile.Title = "Save as";
                saveFile.Filter = "*.txt|";
                if (saveFile.ShowDialog() == DialogResult.OK)
                {
                    FileStream f = new FileStream(saveFile.FileName.ToString() + ".txt", FileMode.Create);
                    StreamWriter srw = new StreamWriter(f);
                    srw.Write(tbxHeso.Text);
                    srw.WriteLine();
                    srw.Write(tbxKhoiluong.Text);
                    srw.WriteLine();
                    srw.Write(tbxLuc.Text);
                    srw.WriteLine();
                    srw.Write(tbxVantoc.Text);
                    srw.WriteLine();
                    srw.Close();
                    f.Close();
                }
            }           
        }

        private void btLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFile.RestoreDirectory = true;
            DialogResult result = openFile.ShowDialog();
            if (result == DialogResult.OK)
            {
                string filename = openFile.FileName;
                string[] lines = File.ReadAllLines(filename);
                tbxHeso.Text = lines[0].ToString();
                tbxKhoiluong.Text = lines[1].ToString();
                tbxLuc.Text = lines[2].ToString();
                tbxVantoc.Text = lines[3].ToString();
            }
        }
    }
}
